package com.tz.movie.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
 
import com.tz.movie.domain.User;
import com.tz.movie.domain.UserExample;
import com.tz.movie.domain.status;
 
import com.tz.movie.mapper.UserMapper;

import org.omg.CORBA.UserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
public class UploadController {

  @javax.annotation.Resource
  private ResourceLoader resourceLoader;

  @RequestMapping("/photo/upload")
  public String upload(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest req) {
    String name = "";
    try {
      // multipartFile.getOriginalFilename() 获取文件原始名称
      // new File(multipartFile.getOriginalFilename()) 根据获取到的原始文件名创建目标文件
      // multipartFile.transferTo() 将收到的文件传输到目标文件中
      String fileName = System.currentTimeMillis() + multipartFile.getOriginalFilename();
      name = "uploaded" + File.separator + fileName;
      String destFileName = req.getServletContext().getRealPath("") + name;
      File destFile = new File(destFileName);
      destFile.getParentFile().mkdirs();
      multipartFile.transferTo(destFile);
      // 6.把文件名放在model里，以便后续显示用
    } catch (IOException e) {
      e.printStackTrace();
    }
    return name;
  }

 

}